package com.wuseguang.springcloud.controller;


import com.wuseguang.springcloud.Classcardservice.ClassCardService;
import com.wuseguang.springcloud.Filter.RequestParameterWrapper;
import com.wuseguang.springcloud.service.ClassPicturesService;
import com.wuseguang.springcloud.util.CodeUtil;
import com.wuseguang.springcloud.util.Result;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;
import java.util.List;

/**
 * <p>
 * 班级(相片/图片) 前端控制器
 * </p>
 *
 * @author
 * @since 2020-01-07
 */
@RestController
@RequestMapping("/classPictures")
public class ClassPicturesController {


    private Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private ClassPicturesService classPicturesServiceImpl;

    //推送接口
    @Autowired
    private ClassCardService classCardService;

    /**
     * 添加班级图片
     *
     * @param request
     * @param name        名称/标题
     * @param show        是否展示在班牌首页  0不显示，1显示
     * @param classId     班级id
     * @param pictyresUrl 图片URL
     * @return
     */
    @RequestMapping("/addClassPictures")
    public Result addClassPictures(RequestParameterWrapper request,
                                   @RequestParam(value = "name", required = false) String name,
                                   @RequestParam(value = "show", required = false, defaultValue = "0") Integer show,
                                   @RequestParam(value = "pshow", required = false, defaultValue = "0") Integer pshow,
                                   @RequestParam(value = "classId", required = false) Integer[] classId,
                                   @RequestParam(value = "pictyresUrl", required = false) String pictyresUrl) {
        logger.info("添加班级图片 ClassPicturesController - > addClassPictures");

       /* //判断图片名称是否为空
        if (StringUtils.isEmpty(name)) {
            return new Result(CodeUtil.REQUIRED_VALUE_IS_EMPTY.getCode(), CodeUtil.REQUIRED_VALUE_IS_EMPTY.getMsg() + "图片标题为空");
        }*/
        //判断是否上传了图片
        if (StringUtils.isEmpty(pictyresUrl)) {
            return new Result(CodeUtil.NO_PICTURES_UPLOADE);
        }

        //学校id
        String schoolId = request.getParam("schoolId");
        //教师id
        String teacherId = request.getParam("teacherId");
        //教师名称
        String teacherName = request.getParam("teacherName");
        //学期id
        String termId = request.getParam("termId");

        Result result = classPicturesServiceImpl.addClassPictures(schoolId, name, show,pshow, classId, pictyresUrl, termId, teacherName, teacherId);
        if (classId != null && classId.length > 0) {
            List<Integer> integers = Arrays.asList(classId);
            classCardService.classesAndCards(integers);
        }
        return result;
    }

    /**
     * 置顶图片
     *
     * @param request
     * @param id
     * @return
     */
    @RequestMapping("/roofPlacementPictures")
    public Result roofPlacementPictures(RequestParameterWrapper request,
                                        @RequestParam(value = "id", required = false) String id) {
        logger.info("图片置顶到班级 ClassPicturesController - > addClassPictures");
        //教师id
        String teacherId = request.getParam("teacherId");
        //获取是不是管理员
        String admin = request.getParam("admin");
        //学校id
        String schoolId = request.getParam("schoolId");

        return classPicturesServiceImpl.roofPlacementPictures(id, teacherId, admin, schoolId);
    }

    /**
     * 删除图片
     *
     * @param request
     * @param id      图片id
     * @return
     */
    @RequestMapping("/deletePictures")
    public Result deletePictures(RequestParameterWrapper request,
                                 @RequestParam(value = "id", required = false) String id) {
        //教师id
        String teacherId = request.getParam("teacherId");
        //获取是不是管理员
        String admin = request.getParam("admin");
        logger.info("删除图片  id = {}", id);
        return classPicturesServiceImpl.deletePictures(id, teacherId, admin);
    }

    /**
     * 查询班级图片
     *
     * @param request
     * @param classId 班级id
     * @return
     */
    @RequestMapping("/findClassPictureList")
    public Result findClassPictureList(RequestParameterWrapper request,
                                       @RequestParam(value = "classId", required = false) String[] classId,
                                       @RequestParam(value = "indexNo", required = false, defaultValue = "1") Integer indexNo,
                                       @RequestParam(value = "rows", required = false, defaultValue = "20") Integer rows) {

        logger.info("查询班级图片 ClassPicturesController - > findClassPictureList");

        //学期id
        String termId = request.getParam("termId");
        //学校id
        String schoolId = request.getParam("schoolId");
        //管理员
        String admin = request.getParam("admin");
        //教师id
        String teacherId = request.getParam("teacherId");

        return classPicturesServiceImpl.findClassPictureList(classId, termId, schoolId, indexNo, rows, admin, teacherId);
    }


    /**
     * 查询图片视频通知统计
     * @param request
     * @param findType 0本周,1上周,2本月,3自定义
     * @param startTime 开始时间
     * @param endTime 结束时间
     * @return
     */
    @RequestMapping("/findPicturesVideoNotice")
    public Result findPicturesVideoNotice(RequestParameterWrapper request,
                                          @RequestParam(value = "findType", required = false,defaultValue = "0") Integer findType,
                                          @RequestParam(value = "startTime", required = false) String startTime,
                                          @RequestParam(value = "endTime", required = false) String endTime){
        logger.info("查询图片视频通知统计");
        //学校id
        String schoolId = request.getParam("schoolId");
        return classPicturesServiceImpl.findPicturesVideoNotice(schoolId,findType,startTime,endTime);
    }
}

